package com.zulu.karaoke.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.zulu.karaoke.model.SongModel;

public class SongTableHelper {
//	private static final String TAG = "Zulukara_Database";
	private DatabaseHelper mDbHelper;
//	private Context mContext;
	
	public SongTableHelper(DatabaseHelper dbHelper)
	{
		mDbHelper = dbHelper;
//		mContext = context;
	}
	
	public List<String> getAllNumber() {
		
		List<String> lstNumber = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},null,
					null, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));	
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));	
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstNumber.add(song.getSongNumber());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstNumber;
	}
	
	public List<String> getAllNumberOnFilter(String filter) {
		
		List<String> lstNumber = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH
							+ " LIKE ? OR "
							+ DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH
							+ " LIKE ? OR "
							+ DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH
							+ " LIKE ? ",
						new String[] { filter + "%" , filter + "%" , filter + "%" }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));	
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));	
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));	
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));					
				
					lstNumber.add(song.getSongNumber());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstNumber;
	}
	
	public List<String> getAllNumberBaseCalegoryAlpha(String category, String alpha) {
		
		List<String> lstNumber = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY
							+ "=? and "
							+ DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR
							+ "=?",
					new String[] { category , alpha }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));	
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstNumber.add(song.getSongNumber());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstNumber;
	}
	
	public List<String> getAllNumberBaseCalegory(String category) {
		
		List<String> lstNumber = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY
							+ "=? ",
					new String[] { category }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));	
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstNumber.add(song.getSongNumber());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstNumber;
	}
	
	public List<String> getAllFavouriteNumber() {
		
		List<String> lstFavouriteNumber = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE
							+ "=? ",
					new String[] { "1" }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstFavouriteNumber.add(song.getSongNumber());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstFavouriteNumber;
	}
	
	public List<String> getAllFavouriteBegin() {
		
		List<String> lstFavouriteBegin = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE
							+ "=? ",
					new String[] { "1" }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstFavouriteBegin.add(song.getSongBegin());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstFavouriteBegin;
	}
	
	public List<String> getAllTitle() {
		
		List<String> lstTitle = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},null,
					null, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstTitle.add(song.getSongTitle());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstTitle;
	}
	
	public List<String> getAllTitleOnFilter(String filter) {
		
		List<String> lstTitle = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH
							+ " LIKE ? OR "
							+ DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH
							+ " LIKE ? OR "
							+ DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH
							+ " LIKE ? ",
						new String[] { filter + "%" , filter + "%" , filter + "%" }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstTitle.add(song.getSongTitle());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstTitle;
	}
	
	public List<String> getAllTitleBaseCategory(String category) {
		
		List<String> lstTitle = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY
							+ "=? ",
					new String[] { category }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstTitle.add(song.getSongTitle());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstTitle;
	}
	
	public List<String> getAllTitleBaseCategoryAlpha(String category, String alpha) {
		
		List<String> lstTitle = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY
							+ "=? and "
							+ DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR
							+ "=?",
					new String[] { category , alpha }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstTitle.add(song.getSongTitle());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstTitle;
	}
	
	public List<String> getAllFavouriteTitle() {
		
		List<String> lstFavouriteTitle = new ArrayList<String>();

		SQLiteDatabase db = null;
		Cursor c = null;
		SongModel song = new SongModel();
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE
							+ "=? ",
					new String[] { "1" }, // argument[]
					null, // groupBy
					null, // having
					DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH + " ASC" // order
			);
			if (c.moveToFirst()) {
				do {
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					lstFavouriteTitle.add(song.getSongTitle());
				} while (c.moveToNext());
			} else {
				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstFavouriteTitle;
	}
	
	public List<SongModel> getAllFavouriteSong() {
		
		List<SongModel> lstFavouriteSong = new ArrayList<SongModel>();

		SQLiteDatabase db = null;
		Cursor c = null;
		
		try {
			db = mDbHelper.getReadableDatabase();
			SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
			qb.setTables(DatabaseDefinition.MySongList.TABLE);
			c = qb.query(
					db, // Database
					new String[] {
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE
							+ "=? ",
					new String[] { "1" }, // argument[]
					null, // groupBy
					null, // having
					null // order
			);
			if (c.moveToFirst()) {
				do {
					SongModel song = new SongModel();
					
					song.setSongNumber(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
					song.setSongTitle(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
					song.setSongBegin(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
					song.setSongFavourite(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
					song.setSongComposer(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
					song.setSongCategory(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
					song.setSongTitleSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
					song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
					song.setSongComposerSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
					song.setSongBeginSearch(c.getString(c.getColumnIndex(
							DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
					
					lstFavouriteSong.add(song);
					
				} while (c.moveToNext());
			} else {
//				song = null;
			}
		}finally {
			if (db != null) {
				db.close();
			}
			if (c != null) {
				c.close();
			}
		}
		
		return lstFavouriteSong;
	}
	
	public SongModel getSongObjectBaseNumber(String number) {
		SongModel song = new SongModel();
		synchronized (this) {
			SQLiteDatabase db = null;
			Cursor c = null;
			try {
				db = mDbHelper.getReadableDatabase();
				SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
				qb.setTables(DatabaseDefinition.MySongList.TABLE);
				c = qb.query(
						db, // Database
						new String[] {
								DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
								DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
								DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
						DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER
								+ "=? ",
						new String[] { number }, // argument[]
						null, // groupBy
						null, // having
						null // order
				);
				if (c.moveToFirst()) {
					do {
						song.setSongNumber(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
						song.setSongTitle(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
						song.setSongBegin(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
						song.setSongFavourite(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
						song.setSongComposer(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
						song.setSongCategory(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
						song.setSongTitleSearch(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
						song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
						song.setSongComposerSearch(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
						song.setSongBeginSearch(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
						
					} while (c.moveToNext());
				} else {
					song = null;
				}
			}finally {
				if (db != null) {
					db.close();
				}
				if (c != null) {
					c.close();
				}
			}
		}
		return song;
	}
	
	public String getSongTitleBaseNumber(String number) {
		String title = "";
		synchronized (this) {
			SQLiteDatabase db = null;
			Cursor c = null;
			SongModel song = new SongModel();
			try {
				db = mDbHelper.getReadableDatabase();
				SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
				qb.setTables(DatabaseDefinition.MySongList.TABLE);
				c = qb.query(
						db, // Database
						new String[] {
								DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
								DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
								DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH},
						DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER
								+ "=? ",
						new String[] { number }, // argument[]
						null, // groupBy
						null, // having
						null // order
				);
				if (c.moveToFirst()) {
					do {
						song.setSongNumber(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER)));
						song.setSongTitle(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE)));		
						song.setSongBegin(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN)));	
						song.setSongFavourite(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE)));
						song.setSongComposer(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER)));	
						song.setSongCategory(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY)));	
						song.setSongTitleSearch(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH)));
						song.setSongTitleFirstChar(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR)));
						song.setSongComposerSearch(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH)));
						song.setSongBeginSearch(c.getString(c.getColumnIndex(
								DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH)));
						title = song.getSongTitle();
						
					} while (c.moveToNext());
				} else {
					song = null;
					title = " ";
				}
			}finally {
				if (db != null) {
					db.close();
				}
				if (c != null) {
					c.close();
				}
			}
		}
		return title;
	}
	
	public void addSong(SongModel songModel) {
		SQLiteDatabase db = null;
		synchronized (this) {
			try {
				db = mDbHelper.getWritableDatabase();
				try {
					
					db.beginTransaction();
					ContentValues values = new ContentValues();
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							songModel.getSongNumber());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							songModel.getSongTitle());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							songModel.getSongBegin());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							songModel.getSongFavourite());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							songModel.getSongComposer());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							songModel.getSongCategory());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							songModel.getSongTitleSearch());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							songModel.getSongTitleFirstChar());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							songModel.getSongComposerSearch());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH,
							songModel.getSongBeginSearch());
					
					db.insertOrThrow(
							DatabaseDefinition.MySongList.TABLE,
							null, values);
					db.setTransactionSuccessful();
					
				}catch (SQLException e) {
					e.printStackTrace();
				} finally {
					db.endTransaction();
				}
			}finally {
				if (db != null) {
					db.close();
				}
			}
		}
	}
	
	public void updateSong(SongModel songModel) {

		SQLiteDatabase db = null;
		synchronized (this) {
			try {
				db = mDbHelper.getWritableDatabase();
				try {
					
					db.beginTransaction();
					ContentValues values = new ContentValues();
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER,
							songModel.getSongNumber());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_TITLE,
							songModel.getSongTitle());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN,
							songModel.getSongBegin());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_FAVOURITE,
							songModel.getSongFavourite());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER,
							songModel.getSongComposer());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_CATEGORY,
							songModel.getSongCategory());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_SEARCH,
							songModel.getSongTitleSearch());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_TITLE_FIRST_CHAR,
							songModel.getSongTitleFirstChar());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_COMPOSER_SEARCH,
							songModel.getSongComposerSearch());
					
					values.put(DatabaseDefinition.MySongList.COLUMN_SONG_BEGIN_SEARCH,
							songModel.getSongBeginSearch());
					
					db.update(DatabaseDefinition.MySongList.TABLE, values, 
							DatabaseDefinition.MySongList.COLUMN_SONG_NUMBER + "=?", 
							new String[] {songModel.getSongNumber()}
							);
							
					db.setTransactionSuccessful();
					
				}catch (SQLException e) {
					e.printStackTrace();
				} finally {
					db.endTransaction();
				}
			}finally {
				if (db != null) {
					db.close();
				}
			}
		}
	}
}
